package com.hongxin.nw.netty.mq_info;

import com.alibaba.fastjson.JSONObject;
import com.hongxin.nw.netty.config.ApplicationContextProvider;
import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.utils.JSONUtils;
import com.hongxin.nw.netty.utils.MqSender;
import com.hongxin.nw.netty.values.Catalogue;
import com.hongxin.nw.netty.values.StaticValue;
import lombok.extern.slf4j.Slf4j;

/**
 * @remark 消息队列推送
 * @author: GuoXiaolin
 * @date 2019/6/24 10:38
 * @Version 1.0
 * @Descibe
 */
@Slf4j
public class Mq{

    private IMq iMq ;

    public Mq(IMq iMq){
        this.iMq = iMq;
    }

    public JSONObject sendMq(Transfer transfer) throws Exception {

        MqSender mqSender = ApplicationContextProvider.getBean(MqSender.class);
        JSONObject json = this.iMq.sendMqInfo(transfer);
        if (json.getIntValue(JSONUtils.JSON_CODE) == 0){
            JSONObject data = json.getJSONObject(JSONUtils.JSON_DATA);
            data.put(Catalogue.TYPE, Catalogue.SOUTH);
            JSONObject jm = mqSender.send(transfer.getSendType(), data);
            if (jm.getIntValue(JSONUtils.JSON_CODE) == 0){
                return JSONUtils.getRoot(0, json.getString(JSONUtils.JSON_MSG), data);//mq发送成功
            }
            return JSONUtils.getRoot(-1, jm.getString(JSONUtils.JSON_MSG), data);//mq发送失败
        }
        return JSONUtils.getRoot(-2, json.getString(JSONUtils.JSON_MSG), StaticValue.VAL_NULL);//不需要发送mq
//        可使用线程发送MQ提高函数响应效率
//        StaticValue.executor.execute(new Runnable() {
//            @Override
//            public void run() {
//                mqSender.send(sendType, sendjson);
//            }
//        });
    }
}
